package ar.edu.unq.wannaplay.persistence.daos;

import java.sql.SQLException;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import ar.edu.unq.wannaplay.persistence.genericdaos.HibernateGenericDao;
import ar.edu.unq.wannaplay.users.User;

public class UserDAO extends HibernateGenericDao<User> {
    private static final long serialVersionUID = 1L;

    @Override
    protected Class<User> getDomainClass() {
        return User.class;
    }

    public User findByUsername(final String username) {
        return (User) this.getHibernateTemplate().execute(new HibernateCallback() {
            @Override
            public User doInHibernate(final Session session) throws HibernateException, SQLException {
                Criteria idCriteria = session.createCriteria(UserDAO.this.getDomainClass());
                idCriteria.add(Restrictions.eq("username", username));
                return (User) idCriteria.uniqueResult();
            }
        });
    }
}
